package com.qianchang.microfinance.view.hsdialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.qianchang.microfinance.R;

/**
 * Created by Administrator on 2018/1/14.
 */

public class BankCardDetailDialog extends DialogFragment implements View.OnClickListener{

    private static final String TAG = "Dialog2";
    private OnDialogClickListener onDialogClickListener;
    private Dialog mDialog;



    public static BankCardDetailDialog newInstance() {
        return new BankCardDetailDialog();
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM; // 显示在底部
        params.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度填充满屏
        window.setAttributes(params);

        // 这里用透明颜色替换掉系统自带背景
        int color = ContextCompat.getColor(getActivity(), android.R.color.transparent);
        window.setBackgroundDrawable(new ColorDrawable(color));

    }


    View dialogView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mDialog = getDialog();
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题栏
        Bundle bundle = getArguments();
        boolean hasData = bundle.getBoolean("isEmptydata",false);
        setCanceledOnTouchOutside(true);
        dialogView = inflater.inflate(R.layout.dialog_bankcarddetail_layout, container, false);
        LinearLayout linearLayout = (LinearLayout) dialogView.findViewById(R.id.linear_mang_id);
        RelativeLayout addRelat = dialogView.findViewById(R.id.add_bankcard_rl_id);
        addRelat.setOnClickListener(this);
        RelativeLayout unbundledRelat = dialogView.findViewById(R.id.unbundled_bankcard_rl_id);
        unbundledRelat.setOnClickListener(this);
        RelativeLayout changepsw = dialogView.findViewById(R.id.change_psw_id);
        changepsw.setOnClickListener(this);
        RelativeLayout phoneNub = dialogView.findViewById(R.id.change_phoneNub_id);
        phoneNub.setOnClickListener(this);

        if(hasData){
            addRelat.setVisibility(View.GONE);
            unbundledRelat.setVisibility(View.VISIBLE);
            linearLayout.setVisibility(View.VISIBLE);
        }else{
            addRelat.setVisibility(View.VISIBLE);
            unbundledRelat.setVisibility(View.GONE);
            linearLayout.setVisibility(View.GONE);
        }
        startUpAnimation(dialogView);
        return dialogView;
    }


    private void startUpAnimation(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);
    }

    private void startDownAnimation(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);

        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        slide.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(slide);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add_bankcard_rl_id:               //添加银行卡绑定
                startDownAnimation(dialogView);
                if(onDialogClickListener != null){
                    onDialogClickListener.addBankCard();
                }
                break;
            case R.id.unbundled_bankcard_rl_id:         //解绑银行卡
                startDownAnimation(dialogView);
                if(onDialogClickListener != null){
                    onDialogClickListener.unbundledBankCard();
                }
                break;
            case R.id.change_psw_id:                    //修改支付密码
                startDownAnimation(dialogView);
                if(onDialogClickListener != null){
                    onDialogClickListener.changePaypsw();
                }
                break;
            case R.id.change_phoneNub_id:               //修改银行卡预留手机号码
                startDownAnimation(dialogView);
                if(onDialogClickListener != null){
                    onDialogClickListener.changePhoneNub();
                }

                break;
            default:
                break;
        }
    }

    public interface OnDialogClickListener {
        void addBankCard();

        void unbundledBankCard();

        void changePaypsw();

        void changePhoneNub();
    }


    public void setCanceledOnTouchOutside(boolean cancel) {
        if (mDialog != null) {
            mDialog.setCanceledOnTouchOutside(cancel);
        }
    }

    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
        this.onDialogClickListener = onDialogClickListener;
    }

}
